/*
 * @(#)SimpleFormatter.java 1.15 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package grimbo.goodwinfooty.util.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * Removed the line separator output.
 * 
 * Print a brief summary of the LogRecord in a human readable format. The
 * summary will typically be 1 or 2 lines.
 * 
 * @version 1.15, 11/17/05
 * @since 1.4
 */

public class SimpleFormatter extends Formatter {

    private final static String format = "[{0,date,short}-{0,time,full}]";
    private final static String lineSeparator = System.getProperty("line.separator");
    /**
     * use instance variable as format method is synchronized.
     */
    private DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss:SSS");
    private Date dat = new Date();
    private Object args[] = new Object[1];

    /**
     * Format the given LogRecord.
     * 
     * @param record
     *            the log record to be formatted.
     * @return a formatted log record
     */
    public synchronized String format(LogRecord record) {
        StringBuffer sb = new StringBuffer();
        // Minimize memory allocations here.
        dat.setTime(record.getMillis());
        sb.append("[");
        sb.append(dateFormat.format(dat));
        sb.append("] ");
        if (record.getSourceClassName() != null) {
            sb.append(record.getSourceClassName());
        } else {
            sb.append(record.getLoggerName());
        }
        if (record.getSourceMethodName() != null) {
            sb.append(" ");
            sb.append(record.getSourceMethodName());
        }
        sb.append(" ");
        String message = formatMessage(record);
        sb.append(record.getLevel().getLocalizedName());
        sb.append(": ");
        sb.append(message);
        sb.append(lineSeparator);
        if (record.getThrown() != null) {
            try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                record.getThrown().printStackTrace(pw);
                pw.close();
                sb.append(sw.toString());
            } catch (Exception ex) {
            }
        }
        return sb.toString();
    }
}
